package com.qc.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qc.bus.dao.BusSalesDao;
import com.qc.bus.dao.BusSalesbackDao;
import com.qc.bus.entity.*;
import com.qc.bus.service.BusCustomerService;
import com.qc.bus.service.BusGoodsService;
import com.qc.bus.service.BusSalesbackService;
import com.qc.bus.vo.SalesbackVo;
import com.qc.sys.common.ActiveUser;
import com.qc.sys.common.DataGridView;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * (BusSalesback)表服务实现类
 *
 * @author AltmanQccc
 * @since 2020-09-11 10:25:21
 */
@Service
public class BusSalesbackServiceImpl extends ServiceImpl<BusSalesbackDao, BusSalesback> implements BusSalesbackService {

    @Autowired
    private BusGoodsService busGoodsService;

    @Autowired
    private BusCustomerService customerService;

    @Autowired
    private BusSalesbackDao salesbackDao;

    @Autowired
    private BusSalesDao salesDao;

    @Override
    public DataGridView queryAllSalesback(SalesbackVo salesbackVo) {
        IPage<BusSalesback> page = new Page<>(salesbackVo.getPage(),salesbackVo.getLimit());
        QueryWrapper<BusSalesback> qw = new QueryWrapper<>();
        qw.eq(salesbackVo.getGoodsid()!=null,"goodsid",salesbackVo.getGoodsid());
        qw.eq(salesbackVo.getCustomerid()!=null,"customerid",salesbackVo.getCustomerid());
        qw.ge(salesbackVo.getStartTime()!=null,"salesbacktime",salesbackVo.getStartTime());
        qw.le(salesbackVo.getEndTime()!=null,"salesbacktime",salesbackVo.getEndTime());
        this.salesbackDao.selectPage(page,qw);
        List<BusSalesback> records = page.getRecords();
        for (BusSalesback salesback : records) {
            if (salesback.getCustomerid()!=null){
                BusCustomer customer = this.customerService.getById(salesback.getCustomerid());
                salesback.setCustomername(customer.getCustomername());
            }
            if (salesback.getGoodsid()!=null){
                BusGoods goods = this.busGoodsService.getById(salesback.getSalesid());
                salesback.setGoodsname(goods.getGoodsname());
                salesback.setSize(goods.getSize());
            }
        }
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @Override
    public BusSalesback saveSalesback(BusSalesback salesback) {
        Integer salesId=salesback.getSalesid();
        BusSales sales = this.salesDao.selectById(salesId);
        salesback.setCustomerid(sales.getCustomerid());
        salesback.setPaytype(sales.getPaytype());
        salesback.setSalesbacktime(new Date());
        salesback.setSalebackprice(sales.getSaleprice());
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        salesback.setOperateperson(activeUser.getUser().getName());
        this.salesbackDao.insert(salesback);
        //增加库存
        BusGoods goods=this.busGoodsService.getById(sales.getGoodsid());
        goods.setNumber(goods.getNumber()+salesback.getNumber());
        this.busGoodsService.UpdateGoods(goods);
        //更新销售单信息
        sales.setNumber(sales.getNumber()-salesback.getNumber());
        salesDao.updateById(sales);
        return salesback;

    }
}